package Management;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import Error.Errormanager;
import Management.fileIO.*;

public class Rules {

	public static 	String directionary = "./property/animations.rules";		//规则所在目录
	public String  	rulename;			//规则的名字
	public String[] key = null; 		//关键字的数组
	public String	mainPath = null;	//主目录
	public int		starNumber = 0;	//规则中*号表示在数组中的位置
	public int 		ID;
	
	private MyFileReader	myFileReader = new MyFileReader(directionary);
	private	Errormanager	error = new Errormanager(); 
	
	/**
	 * 构造方法
	 */
	public Rules(int ID) {
		this.ID = ID;
		String[] rules = null;
		try {
			rules = myFileReader.readl(directionary);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			error.setNumber(Errormanager.FileNotFound);
			error.show();
		} catch (IOException e) {
			e.printStackTrace();
			error.setNumber(Errormanager.FileNotFound);
			error.show();
		}
		setKey(rules[ID-1]);
	}

	/**
	 * name：	getKey()
	 * in：     	null
	 * out：     	null
	 * work：     	获得key字数组
	 */
	public String[] getKey() {
		return key;
	}

	/**
	 * name：	setKey(String rules)
	 * in：		string rule
	 * out:		null
	 * work:	根据规则设置key数组
	 */
	public void setKey(String rules) {
		String[] change = rules.split("[|]");
//		for(String i:change) {
//			System.out.print(i);
//		}
//		System.out.print(change[6]);
		key = change;
	}

	/**
	 * name:	getRuleNumber()
	 * in:		null
	 * out:		int ruleNumber
	 * work:	获得	ruleNumber的号（必需先设置key）
	 */
	public String getRuleName() {
		rulename = key[0];
		return rulename;
	}

	/**
	 * name:	getMainPath()
	 * in:		null
	 * out:		mainPath
	 * work:	获得mainPath，即主目录
	 */
	public String getMainPath() {
		mainPath = new String(key[1]);
		mainPath = new File(mainPath).getAbsolutePath();
		return mainPath;
	}

	/**
	 * name:	getFolderNumber()
	 * in:		null
	 * out:		int folderNumber
	 * work:	获得folderNumber（必需先设置key）
	 */
	public int getStarNumber() {
		for( int i=0; i<key.length; i++) {
			if( key[i].equals("*")) {
				starNumber = i;
			}
		}
		
		return starNumber;
	}
	
	public void changeMainPathTo(String path) {
		String[] destination = key;
		destination[1] = path;
		String destiny = "";
		
		//make the String[] become a String;
		for (String s: destination) {
			if (!s.equals("")) {
				if (destiny.equals("")) {
					destiny = s;
				}
				else {
					destiny = destiny + "|" + s;
				}
			}
		}
		destiny = destiny.trim();
//		System.out.println("destiny:" + destiny);
		
		String[] rules = null;
		try {
			rules = myFileReader.readl(directionary);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			error.setNumber(Errormanager.FileNotFound);
			error.show();
		} catch (IOException e) {
			e.printStackTrace();
			error.setNumber(Errormanager.FileNotFound);
			error.show();
		}
		String[] right = rules;
		right[ID-1] = destiny;
		for (int i = 0; i < rules.length; i++) {
			try {
				myFileReader.delete(directionary, i+1);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		for (int i = 0; i < right.length; i++) {
			try {
				myFileReader.append(directionary, right[i]);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
